<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/* ****************************************************************************
  NAIYUMIE KING WANG JJANG Front End Developer <naiyumie@gmail.com>
  FileType:CRLF, Encording:UTF-8, Tab&Space:4/4
  ------------------------------------------------------------------------
  계정 권한
**************************************************************************** */

class Authentication extends N_Controller {

	/***************************************************
	 * preprocess
	 *     constructor
	 *     function
	 *     declaration
	 ***************************************************/
	public function __construct(){
		parent::__construct();

		$this->nUriExtend = array(
			'loginSeg' => 'loginCheck',
			'logoutSeg' => 'logoutCheck'
		);
		$this->nUri = array_merge($this->nUri, $this->nUriExtend);

		$this->load->model('AuthenticationM');
		$this->load->model('MembersM');
	}

	public function index() {
		$this->login();
	}



	/***************************************************
	 * view control : Layouts
	 ***************************************************/
	public function login(){
		$this->tilesHeader();
		$this->load->view('vLoginSample');
		$this->tilesFooter();
	}

	public function tmpLogout(){
		$this->tilesHeader();
		$this->load->view('vLogoutSample');
		$this->tilesFooter();
	}


	/***************************************************
	 * ajax process : loginCheck
	 ***************************************************/
	public function loginCheck(){
		# local variables
		$retVal = '';
		$mode = '';
		$modelworks = array();
		$data = array();

		# decoding and set mode
		//$data['tmp___posts'] = $this->input->post();
		$this->validatedValues = $this->nvalidation->postAesDecodes();
		//echo "validatedValues>>"; print_r($this->validatedValues);
		$mode = trim($this->nurl->decrypt($this->input->get('mode')));

		# make cookie
		if($this->validatedValues['ck_remember'] == 'y'){
			$cookie = array(
				'name'   => 'uId',
				'value'  => $this->validatedValues['uq_member_id'],
				'expire' => '86500',
				'prefix' => 'ck_',
			);
			$this->input->set_cookie($cookie['name'], $cookie['value'], $cookie['expire'], '', '/', $cookie['prefix']);
		}

		# Validation Phase
		if(!$this->nvalidation->basicValidation(array('mode'=>$mode,'validationValueArray'=>$this->validatedValues, 'uq_'=>FALSE))){
			$data['mode'] = $mode;
			$data['process'] = 'validation';
			$data['resultMessage'] = 'fail';
			$data['vStatArray'] = json_encode($this->nvalidation->vStatFailedResultArray);
			$retVal = json_encode($data);
			exit($retVal);
		}
		$modelworks = $this->AuthenticationM->r01($this->validatedValues);
		$data['mode'] = $mode;
		$data['process'] = 'loginCheck';
		$data['resultMessage'] = $modelworks['isLogIn'] ? 'succ' : 'fail';

		# make session
		if($modelworks['isLogIn']){
			$sessionSetData = array(
				'userName' => $modelworks['loginedUserData']['st_name'],
				'authType' => $modelworks['loginedUserData']['fg_auth_type'],
				'dt_joined' => $modelworks['loginedUserData']['dt_joined'],
				'tm_joined' => $modelworks['loginedUserData']['tm_joined'],
				'dt_last_logined' => $modelworks['loginedUserData']['dt_last_logined'],
				'tm_last_logined' => $modelworks['loginedUserData']['tm_last_logined']
			);
			$this->session->set_userdata($sessionSetData);
		}

		# update last logined dt, tm
		$configLastLogin = array(
			'uq_member_id' => $this->validatedValues['uq_member_id'],
			'dt_last_logined' =>getNowDate(),
			'tm_last_logined' =>getNowTime()
		);
		$this->MembersM->u02($configLastLogin);

		# for debug
		$data['tmp___session_all_data'] = print_r($this->session->all_userdata(), TRUE);

		# out put json
		$retVal = json_encode($data);
		exit($retVal);
	}


	/***************************************************
	 * ajax process : logoutCheck
	 ***************************************************/
	public function logoutCheck(){
		$this->session->sess_destroy();
		exit(json_encode(array('resultMessage' => 'succ')));
	}

}
/* End of file */